package com.hanbang.gycatering.property;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.aboutme.AboutMe;
import com.hanbang.gycatering.adpater.BannerAdAdapter;
import com.hanbang.gycatering.adpater.PropertyAdapter;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.BannerAdData;
import com.hanbang.gycatering.bean.PropertyBeanData;
import com.hanbang.gycatering.classify.ClassifyActivity;
import com.hanbang.gycatering.homepage.MainBaseActivity;
import com.hanbang.gycatering.homepage.HomePageActivity;
import com.hanbang.gycatering.myviews.ListViewForScrollView;
import com.hanbang.gycatering.myviews.MyDialog;
import com.hanbang.gycatering.myviews.MyDialog.MODE;
import com.hanbang.gycatering.myviews.MyDialog.OnClickCallback;
import com.hanbang.gycatering.order.MyOrder;
import com.hanbang.gycatering.property.ConvenienceService;
import com.hanbang.gycatering.property.PropertyAnnouncement;
import com.hanbang.gycatering.shoppingcart.ShoppingCart;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.hanbang.gycatering.utils.ShareMethod;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class PropertyService extends MainBaseActivity {
	private LinearLayout ll_one, ll_two, ll_three, ll_four, ll_five;
	private ArrayList<BannerAdData> bannerAdDatas = new ArrayList<BannerAdData>();
	private ArrayList<PropertyBeanData> announcementDatas = new ArrayList<PropertyBeanData>();
	private BannerAdAdapter bannerAdAdapter = null;
	private ListViewForScrollView pslistview;
	private PullToRefreshScrollView psescrollview;
	private PropertyAdapter myadapter;
	private RelativeLayout banner_ad_wyfw;
	// 当前top图片
	private int current = 0;
	private LinearLayout to_propertyfix, to_propertyfeepay, to_myorder,
			to_convenience_service;
	private ImageView call_property;
	private TextView to_property_gg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.propertyservice);
		initView();
		setTheClick();
		getAnnouuncementDatas(true);
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("物业服务");
		((ImageView) findViewById(R.id.top_bar_left_image))
				.setVisibility(View.GONE);
		((ImageView) findViewById(R.id.search)).setVisibility(View.GONE);
		call_property = ((ImageView) findViewById(R.id.search));
		call_property.setVisibility(View.VISIBLE);
		call_property.setImageResource(R.drawable.wydh);
		ll_one = (LinearLayout) findViewById(R.id.bottom_bar_ll1);
		ll_two = (LinearLayout) findViewById(R.id.bottom_bar_ll2);
		ll_three = (LinearLayout) findViewById(R.id.bottom_bar_ll3);
		ll_four = (LinearLayout) findViewById(R.id.bottom_bar_ll4);
		ll_five = (LinearLayout) findViewById(R.id.bottom_bar_ll5);
		to_property_gg = (TextView) findViewById(R.id.to_property_gg);
		to_propertyfix = (LinearLayout) findViewById(R.id.to_propertyfix);
		to_propertyfeepay = (LinearLayout) findViewById(R.id.to_propertyfeepay);
		to_myorder = (LinearLayout) findViewById(R.id.to_myorder);
		to_convenience_service = (LinearLayout) findViewById(R.id.to_convenience_service);
		banner_ad_wyfw = (RelativeLayout) findViewById(R.id.banner_ad_wyfw);
		pslistview = (ListViewForScrollView) findViewById(R.id.pslistview);
		psescrollview = (PullToRefreshScrollView) findViewById(R.id.psescrollview);
		bannerAdAdapter = new BannerAdAdapter(PropertyService.this,
				banner_ad_wyfw, bannerAdDatas);
		bannerAdAdapter.setWidthAndHeight(16, 9);
		myadapter = new PropertyAdapter(PropertyService.this, announcementDatas);
		pslistview.setAdapter(myadapter);

		psescrollview.setMode(Mode.PULL_FROM_START);
		psescrollview.setOnRefreshListener(new OnRefreshListener<ScrollView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				// TODO Auto-generated method stub
				getAnnouuncementDatas(false);
			}
		});
		pslistview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PropertyService.this,
						AnnouncementDetail.class);
				intent.putExtra("announcementId",
						announcementDatas.get(position).getId());
				startActivity(intent);
			}
		});

	}

	private void getAnnouuncementDatas(final boolean isToTop) {
		bannerAdDatas.clear();
		announcementDatas.clear();
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.noticelist
						+ "&type=1&PageIndex=1", new MyRequestCallBack<String>(
						PropertyService.this, getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								JSONObject info = json.getJSONArray("info")
										.getJSONObject(0);
								JSONArray img = info.getJSONArray("img");
								for (int i = 0; i < img.length(); i++) {
									BannerAdData hb = new BannerAdData();
									hb.setPath(img.getJSONObject(i).getString(
											"img"));
									bannerAdDatas.add(hb);
								}
								JSONArray notice = json.getJSONArray("info")
										.getJSONObject(1)
										.getJSONArray("notice");
								for (int j = 0; j < notice.length(); j++) {
									PropertyBeanData pbdata = new PropertyBeanData();
									pbdata.setId(notice.getJSONObject(j)
											.getInt("id"));
									pbdata.setTitle(notice.getJSONObject(j)
											.getString("title"));
									pbdata.setTime(notice.getJSONObject(j)
											.getString("time"));
									announcementDatas.add(pbdata);
								}
								bannerAdAdapter.notifyDataSetChanged();
								bannerAdAdapter.start();
								myadapter.notifyDataSetChanged();

							} else {
								MyToast.show(PropertyService.this, "获取数据失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(PropertyService.this, "获取数据失败",
									Toast.LENGTH_SHORT);
						}
						psescrollview.onRefreshComplete();
						if (isToTop) {
							psescrollview.smoothScrollTo(0, 0);
						}
					}
				});
	}

	private void setTheClick() {
		ll_one.setOnClickListener(myOncClickListener);
		ll_two.setOnClickListener(myOncClickListener);
		ll_three.setOnClickListener(myOncClickListener);
		ll_four.setOnClickListener(myOncClickListener);
		ll_five.setOnClickListener(myOncClickListener);
		to_property_gg.setOnClickListener(myOncClickListener);
		to_propertyfix.setOnClickListener(myOncClickListener);
		to_propertyfeepay.setOnClickListener(myOncClickListener);
		to_myorder.setOnClickListener(myOncClickListener);
		to_convenience_service.setOnClickListener(myOncClickListener);
		call_property.setOnClickListener(myOncClickListener);
	}

	private OnClickListener myOncClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			Intent intent = new Intent();
			switch (view.getId()) {
			case R.id.bottom_bar_ll1:
				intent.setClass(PropertyService.this, HomePageActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll2:
				intent.setClass(PropertyService.this, ClassifyActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll4:
				intent.setClass(PropertyService.this, ShoppingCart.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll5:
				intent.setClass(PropertyService.this, AboutMe.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.to_property_gg:
				intent.setClass(PropertyService.this,
						PropertyAnnouncement.class);
				startActivity(intent);
				break;
			case R.id.to_propertyfix:
				intent.setClass(PropertyService.this, PropertyFix.class);
				startActivity(intent);
				break;
			case R.id.to_propertyfeepay:
				intent.setClass(PropertyService.this, WaitPayPropertyFee.class);
				startActivity(intent);
				break;
			case R.id.to_myorder:
				intent.setClass(PropertyService.this, MyOrder.class);
				startActivity(intent);
				break;
			case R.id.to_convenience_service:
				intent.setClass(PropertyService.this, ConvenienceService.class);
				startActivity(intent);
				break;
			// 拨打物业电话
			case R.id.search:
				MyDialog dialog3 = new MyDialog(PropertyService.this,
						R.style.base_dialog, MODE.CONFIRM);
				dialog3.setTitle("拨号");
				dialog3.setContent("拨打物业电话？");
				dialog3.setOnClickCallback(new OnClickCallback() {

					@Override
					public void onClick(MyDialog dialog, Object number,
							Object content) {
						if ((Integer) number == 2) {
							// TODO Auto-generated method stub
							// 调用系统的拨号服务实现电话拨打功能
							// 封装一个拨打电话的intent，并且将电话号码包装成一个Uri对象传入
							Intent intentcall = new Intent(Intent.ACTION_CALL,
									Uri.parse("tel:15527258455"));
							startActivity(intentcall);// 内部类
						} else if ((Integer) number == 1) {

						}
					}
				});
				dialog3.show();

				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		if (!isLogin(true)) {
			finish();
		}
		ShareMethod.setButton(ll_three, false);

	};

	@Override
	protected void onPause() {
		super.onPause();
		ShareMethod.setButton(ll_three, true);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
